package com.jxzn.locust.controller.admin;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.jxzn.locust.config.FileUploadConfig;
import com.jxzn.locust.dto.ResultDTO;
import com.jxzn.locust.utils.Tools;

@Controller
@RequestMapping("/bg/files")
public class FileUploadController {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    FileUploadConfig config;

    @PostMapping("/upload/image")
    @ResponseBody
    public ResultDTO<?> uploadImage(@RequestParam("file") MultipartFile file) {
        String name = file.getOriginalFilename();
        String suffix = "";
        if (name.indexOf(".") >= 0) {
            suffix = name.substring(name.lastIndexOf("."), name.length());
        }
        if (!checkImageFormat(suffix)) {
            return ResultDTO.businessError("只能上传jpg或者png格式的图片");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/");
        String uri = sdf.format(Calendar.getInstance().getTime());
        File folder = new File(config.getUploadFolder() + uri);
        if (!folder.isDirectory()) {
            folder.mkdirs();
        }

        String newName = Tools.getUUID() + suffix;
        File outFile = new File(folder, newName);
        try {
            file.transferTo(outFile);
            String url = config.getFileUrl() + uri + newName;
            return ResultDTO.success(url);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return ResultDTO.error();
    }

    @PostMapping("/upload/file")
    @ResponseBody
    public ResultDTO<?> uploadFile(@RequestParam("file") MultipartFile file) {
        String name = file.getOriginalFilename();
        String suffix = "";
        if (name.indexOf(".") >= 0) {
            suffix = name.substring(name.lastIndexOf("."), name.length());
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/");
        String uri = sdf.format(Calendar.getInstance().getTime());
        File folder = new File(config.getUploadFolder() + uri);
        if (!folder.isDirectory()) {
            folder.mkdirs();
        }

        String newName = Tools.getUUID() + suffix;
        File outFile = new File(folder, newName);
        try {
            file.transferTo(outFile);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        String url = config.getFileUrl() + uri + newName;
        Map<String, String> result = new HashMap<String, String>();
        result.put("url", url);
        result.put("name", name);
        return ResultDTO.success(result);
    }

    private boolean checkImageFormat(String suffix) {
        String f = suffix.substring(1);
        String[] fs = config.getImageFormat().split(",");
        for (String s : fs) {
            if (s.equalsIgnoreCase(f)) {
                return true;
            }
        }
        return false;
    }
}
